package com.yangji.uemgupload;


import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;

import com.umeng.analytics.MobclickAgent;

import java.util.HashMap;

/**
 * 生命周期管理
 *
 * @author YangJi
 */
public class ActivityUtils {

    public static HashMap<Activity, Boolean> activityBooleanHashMap = new HashMap<>();

    private static void addActivity(Activity activity) {
        updateActivity(activity, true);
    }

    private static void updateActivity(Activity activity, boolean isFag) {
        if (activity == null) {
            return;
        }
        activityBooleanHashMap.put(activity, isFag);
    }

    private static void deleteActivity(Activity activity) {
        if (activity == null) {
            return;
        }
        if (activityBooleanHashMap.containsKey(activity)) {
            activityBooleanHashMap.remove(activity);
        }
    }


    public static void registerActivity(Application application) {

        application.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                registerFragment(activity);
                addActivity(activity);
            }

            @Override
            public void onActivityStarted(Activity activity) {
                updateActivity(activity, true);
            }

            @Override
            public void onActivityResumed(Activity activity) {
                updateActivity(activity, true);
                UMengStatistics.onResume(activity);
                MobclickAgent.onResume(activity);
            }

            @Override
            public void onActivityPaused(Activity activity) {
                UMengStatistics.onPause(activity);
                updateActivity(activity, false);
                MobclickAgent.onPause(activity);
            }

            @Override
            public void onActivityStopped(Activity activity) {
                deleteActivity(activity);
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
            }

            @Override
            public void onActivityDestroyed(Activity activity) {
                deleteActivity(activity);
            }
        });

    }

    private static void registerFragment(Activity activity) {
        if (activity instanceof FragmentActivity) {
            FragmentActivity fa = (FragmentActivity) activity;
            fa.getSupportFragmentManager().registerFragmentLifecycleCallbacks(new FragmentManager.FragmentLifecycleCallbacks() {
                @Override
                public void onFragmentResumed(FragmentManager fm, Fragment f) {
                    super.onFragmentResumed(fm, f);
                    UMengStatistics.onResume(f);
                }

                @Override
                public void onFragmentPaused(FragmentManager fm, Fragment f) {
                    super.onFragmentPaused(fm, f);
                    UMengStatistics.onPause(f);
                }
            }, true);

        }
    }

    /**
     * 当前App是否处于打开状态
     *
     * @return
     */
    public static boolean isOpenApp() {
        return activityBooleanHashMap.size() > 0;
    }
}
